package com.qdsg.ysg.doctor.ui.adapter;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.ui.dialog.AddExtraDialog;
import com.qdsg.ysg.doctor.ui.dialog.ExtraChargeDialog;
import com.rest.response.NurseItemsVo;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description
 * Created by lbj on 2022/4/9 17:17
 */

public class AddExtraAdapter extends RecyclerView.Adapter<AddExtraAdapter.ViewHolder> {


    private AppCompatActivity context;
    private List<NurseItemsVo.ConsumablesItemVO> mList;

    public AddExtraAdapter(Activity context, List<NurseItemsVo.ConsumablesItemVO> list) {
        this.context = (AppCompatActivity) context;
        this.mList = list;
    }


    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        ViewHolder myHolder;
        myHolder = new ViewHolder( LayoutInflater.from( context ).inflate( R.layout.item_extra, parent, false ) );
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NotNull ViewHolder holder, final int position) {


        holder.tvChooseExtra.setText( mList.get( position ).consumables );
        holder.ivDelete.setOnClickListener( v -> {
            mList.remove( position );
            notifyDataSetChanged();
        } );


        holder.tvChooseExtra.setOnClickListener( v -> showExtraChargeDialog( position ) );
        holder.etExtraNum.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                AddExtraDialog.setAllPrice();
                mList.get( position ).number = s.toString().trim();
            }
        } );

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_choose_extra)
        TextView tvChooseExtra;
        @BindView(R.id.et_extra_num)
        EditText etExtraNum;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;

        public ViewHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }
    }

    private void showExtraChargeDialog(final int position) {
        ExtraChargeDialog mExtraChargeDialog = ExtraChargeDialog.newInstance("1");
        mExtraChargeDialog.setOnItemClickListener( (consumablesCode, consumables, price) -> {
            mList.get( position ).consumablesCode = consumablesCode;
            mList.get( position ).consumables = consumables;
//            mList.get( position ).price = UtilString.mul(  Double.parseDouble( String.valueOf( price ) ), 100 );
            mList.get( position ).price = price;
            AddExtraDialog.setAllPrice();
            notifyDataSetChanged();
        } );
        mExtraChargeDialog.show( context.getSupportFragmentManager(), "mExtraChargeDialog" );
    }
}
